import { createStore, Store } from 'vuex'

// 定义 Task 接口
interface Task {
  name: string
  completed: boolean
}

// 定义 State 接口
interface State {
  tasks: Task[]
}

// 创建 Vuex store
const store: Store<State> = createStore<State>({
  state: {
    tasks: []
  },
  mutations: {
    ADD_TASK(state, task: Task) {
      state.tasks.push(task)
    },
    REMOVE_TASK(state, taskIndex: number) {
      state.tasks.splice(taskIndex, 1)
    }
  },
  actions: {
    addTask({ commit }, task: Task) {
      commit('ADD_TASK', task)
    },
    removeTask({ commit }, taskIndex: number) {
      commit('REMOVE_TASK', taskIndex)
    }
  },
  getters: {
    allTasks(state): Task[] {
      return state.tasks
    }
  }
})

export default store
